package com.mew.slots.core.game.hlsd.room;

import cn.hutool.core.util.RandomUtil;
import com.mew.common.util.GameUtil;
import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.game.fkymr.dto.FkymrPlayMsgs;
import com.mew.slots.core.game.hlsd.bo.HlsdPlayMsgs;
import com.mew.slots.core.game.hlsd.roller.Controlline;
import com.mew.slots.core.game.hlsd.roller.HlsdIli;

import java.util.ArrayList;
import java.util.Arrays;

public class HlsdRoom extends BaseRoom {
    private final int[] ordinaryWeights = new int[]{85,5,5,5};
    private final int[] freeWeights = new int[]{0,70,20,10};

    final int _BONUS = 12;
    final int _SCATTER = 13;
    final int _JP = 14;
    public HlsdRoom(){
        res = new HlsdPlayMsgs();
        row = 4;
        col = 6;
        _WILD = 11;
        spes = new ArrayList<Integer>(){{
            add(_SCATTER);
            add(_BONUS);
        }};
        rate = new int[][]{
                {0, 0,  5, 15, 50, 80},        //9          0
                {0, 0,  5, 15, 50, 80},        //10         1
                {0, 0,  5, 15, 50, 80},        //J          2
                {0, 0,  5, 15, 50, 80},        //Q          3
                {0, 0,  5, 15, 50, 80},        //K          4
                {0, 0,  5, 15, 50, 80},        //A          5

                {0, 0, 10, 25, 100, 150},      //铃铛        6
                {0, 0, 10, 30, 150, 200},      //狗          7
                {0, 0, 10, 30, 150, 200},      //麋鹿        8
                {0, 0, 10, 30, 150, 200},      //绿帽子男     9
                {0, 0, 15, 50, 300, 500},      //雪橇车       10

                {0, 0,  0,   0,  0,   0},      //WILD        11
                {0, 0,  0,   0,  0,   0},      //BONUS 帽子   12
                {0, 0,  0,   0,  0,   0},      //SCATTER    13
                {0, 0,  0,   0,  0,   0},      //JP    14
        };
        alllines=new int[][]{
                {6,7,8,9,10,11},
                {12,13,14,15,16,17},
                {0,1,2,3,4,5},
                {18,19,20,21,22,23},
                {18,13,8,9,16,23},
                {0,7,14,15,10,5},
                {12,7,2,3,10,17},
                {6,13,20,21,16,11},
                {18,13,20,21,16,23},
                {0,7,2,3,10,5},
                {12,19,14,15,22,17},
                {6,1,8,9,4,11},
                {12,7,14,15,10,17},
                {6,13,8,9,16,11},
                {18,19,14,15,22,23},
                {0,1,8,9,4,5},
                {12,13,8,9,16,17},
                {6,7,14,15,10,11},
                {6,7,2,3,10,11},
                {12,13,20,21,16,17},
                {6,13,14,15,16,11},
                {12,7,8,9,10,17},
                {12,19,20,21,22,17},
                {6,1,2,3,4,11},
                {0,7,8,9,10,5},
                {18,13,14,15,16,23},
                {6,19,20,21,22,11},
                {12,1,2,3,4,17},
                {0,13,14,15,16,5},
                {18,7,8,9,10,23},
                {6,7,20,21,10,11},
                {12,13,2,3,16,17},
                {0,1,14,15,4,5},
                {18,19,8,9,22,23},
                {0,1,20,21,4,5},
                {18,19,2,3,22,23},
                {18,13,8,3,4,5},
                {0,7,14,21,22,23},
                {0,1,2,9,16,23},
                {18,19,20,15,10,5},
                {12,7,2,3,4,5},
                {6,13,20,21,22,23},
                {0,1,2,3,10,17},
                {18,19,20,21,16,11},
                {18,13,8,9,10,11},
                {0,7,14,15,16,17},
                {6,7,8,9,16,23},
                {12,13,14,15,10,5},
                {6,19,2,3,22,11},
                {12,1,20,21,4,17}
        };
    }
    @Override
    public void getLinesXml(){

        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getLineInts( free ? 1 : 0,rollerID);
        }
        res.endSlots = endSlots.clone();
        changeWild();
    }

    @Override
    public void getLinesIli(){
        int[] rs;
        if (controlType>0){
            rs = control();
        } else {
            rs = getIliEndSlots();
        }
        res.endSlots  = Arrays.copyOf(rs,col*row);
        endSlots = res.endSlots.clone();
        HlsdPlayMsgs pmRes= (HlsdPlayMsgs) res;
        for (int i = col*row; i < rs.length; i++) {
            if(rs[i]!=-1){
                pmRes.getChangeWilds().add(rs[i]);
                endSlots[rs[i]] = _WILD;
            }

        }
    }

    @Override
    public int[] getIliEndSlots(){
        if(free){
            return iliXml.getSpeints("mfree");
        }
        return iliXml.getLineints(0,rollerID,in,1);

    }

    public void changeWild(){
        HlsdPlayMsgs pmres = (HlsdPlayMsgs) res;

        int cws = free ? GameUtil.weights(freeWeights) : GameUtil.weights(ordinaryWeights);
        while (cws > 0){
            int ri = RandomUtil.randomInt(24);
            if(endSlots[ri] > 10 || ri % 6 == 0 || ri % 6 == 5 || pmres.getChangeWilds().contains(ri)){
                continue;
            }
            pmres.getChangeWilds().add(ri);
            endSlots[ri] = _WILD;
            cws--;
        }
    }

    @Override
    public void freeBouns(){
        int scatterNum = fsdb(_SCATTER);
        if(scatterNum>2){
            res.setFree((byte)1);
            res.setFreeNumber(10);
        }
        int bonusNum= fsdb(_BONUS);
        if(bonusNum>2){
            res.setBouns(1);
        }

        int jpNum= fsdb(_JP);
        if(jpNum>2){
            res.setBouns(2);
        }
    }
    @Override
    public int[] control(){
        Controlline controlline=new Controlline();
        HlsdIli instance = HlsdIli.getInstance();
        switch (controlType) {
            case 1:
                 return bigwin();
            case 2:
                return instance.getSpeints("free");
                /*return controlline.free();*/
            case 3:
                return instance.getSpeints("bonus");
                /*return controlline.bonus();*/
            case 4:
                return instance.getSpeints("jp");
                /*return controlline.jp();*/
            default:
                return instance.getSpeints("0-0");
                /*return controlline.empty();*/
        }
    }
}
